package com.zhixi.pattern.notimportant.mediatorpattern.operation;

/**
 * 中介者
 *
 * @作者 ShuHaoNan
 * @邮箱 shuhaonan1@gmail.com
 * @时间 2020/11/17 9:01
 */
public class Mediator extends AbstractMediator {

    @Override
    public void execute(String str, Object... objects) {
        if (str.equalsIgnoreCase("purchase.buy")) { /* 采购电脑 */
            this.buyComputer((Integer) objects[0]);
        } else if (str.equalsIgnoreCase("sale.sell")) { /* 销售电脑 */
            this.sellComputer((Integer) objects[0]);
        } else if (str.equalsIgnoreCase("stock.clear")) { /* 停止采购 */

        }
    }

    /**
     * 采购电脑
     */
    private void buyComputer(int number) {
        /* 销售状态良好 */
        if (super.sale.getSaleStatus() >= 80) {
            System.out.printf("采购了%d台电脑\n", number);
            super.stock.increase(number);
        } else { /* 销售状态不好 */
            number /= 2;
            System.out.printf("采购了%d台电脑\n", number);
            super.stock.increase(number);
        }
    }

    /**
     * 销售电脑
     */
    private void sellComputer(int number) {
        /* 如果库存不够 */
        if (super.stock.getStockNumber() < number) {
            super.purchase.buyIBMComputer(number);
        }
        System.out.printf("销售了%d台电脑\n", number);
        super.stock.decrease(number);
    }

    /**
     * 停止采购
     */
    private void clearStock() {
        /* 打折销售 */
        super.sale.offSale();
        /* 停止采购 */
        super.purchase.refuseBuyIBM();
    }

}
